﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using System.IO;
using Ic.Model;
using Ic.Dao;
using Ic.Services.band;

namespace Ic.Web.Controllers.Band
{    
    public partial class DataController : Controller
    {
        #region 注入对象

        private IDaoSpring _DaoSpring;
        public IDaoSpring DaoSpring
        {
            get { return _DaoSpring; }
            set { _DaoSpring = value; }
        }

        public bandServices bandServices { get; set; }

        #endregion        

        /// <summary>
        /// Index
        /// </summary>
        public ActionResult Index()
        {
            ViewData.Model = DaoSpring.GetAll<Ic.Model.Band>();
                //.SearchByWhere<Ic.Model.Band>("1=1");
            return View();
        }

        public ActionResult Member(int id)
        {
            ViewData.Model = DaoSpring.SearchByWhere<Ic.Model.BandMember>("BandId=" + id);
            return View();
        }

        public ActionResult BandDetail(int id)
        {
            ViewData.Model = DaoSpring.LoadFromId<Ic.Model.Band>(id);
            return View();
        }

        public ActionResult Append(int id)
        {
            ViewData.Model = DaoSpring.LoadFromId<Ic.Model.Band>(id);
            return View();
        }

        [AcceptVerbs("POST")]
        public ActionResult Append(Bandsman man)
        {
            int bandid = Convert.ToInt32(Request["BandId"]);

            man.User = DaoSpring.LoadFromId<Ic.Model.BandUser>(Convert.ToInt32(Request["UserId"]));
            DaoSpring.Save<Bandsman>(man);

            Ic.Model.Band band = DaoSpring.LoadFromId<Ic.Model.Band>(bandid);

            BandMember bandMember = new BandMember()
                {
                    BandPosition = Request["BandPosition"],
                    IsLeave = false,
                    JoinDate = DateTime.Now,
                    LeaveDate = DateTime.MaxValue,
                    Bandsman = man,
                    Band = band
                };
            if (DaoSpring.Save<Ic.Model.BandMember>(bandMember))
                return RedirectToAction("Index");
            else
                return RedirectToAction("Append", new { id = bandid.ToString() });
            
        }

        public ActionResult Search()
        {
            return View();
        }

        [AcceptVerbs("POST")]
        public ActionResult Search(string query)
        {
            ViewData.Model =
                DaoSpring.FullTextSearch<Ic.Model.BandUser>(query);
            return View();
        }
    }

    public partial class DemoController : Controller
    {
        #region 注入对象

        private IDaoSpring _DaoSpring;
        public IDaoSpring DaoSpring
        {
            get { return _DaoSpring; }
            set { _DaoSpring = value; }
        }
        public bandServices bandServices { get; set; }
        #endregion        
        
        /// <summary>
        /// Index
        /// </summary>
        /// <returns></returns>
        [OutputCache(VaryByParam="none", Duration=600)]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Table页
        /// </summary>  
        [HandleError]
        #region ActionResult TableBinder()
        public ActionResult TableBinder()
        {             
            return View("TableBinder");
        }
        #endregion        


        /* ---------- Create --------- */

        public ActionResult Create()
        {
            return View();
        }

        [AcceptVerbs("POST")]
        [ValidateInput(false)]
        public ActionResult Create(BandUser user)
        {
            if (DaoSpring.Save<BandUser>(user))
                return RedirectToAction("Search"
                    , new { area = "Band", controller = "Data" });
            else
                return View();
        }

        /// <summary>
        /// 验证名字
        /// </summary>
        /// <param name="name"></param>
        [AcceptVerbs("POST")]
        public JsonResult IsUserCanCreate(string UserLogonName)
        {
            JsonResult result = new JsonResult();
            
            if(DaoSpring.SearchByWhere<Ic.Model.BandUser>(string.Format("UserLogonName='{0}'", UserLogonName))
                == null)
                result.Data = false;
            else
                result.Data = true;

            return result;
        }

        /* transaction insert */
        public ActionResult Transaction()
        {
            if (TempData["result"] != null)
                ViewData.Model = TempData["result"];
            return View();
        }

        [AcceptVerbs("POST")]
        public ActionResult TransactionAdd(string ga)
        { 
            TempData["result"] = bandServices.transactionInsert(100);
            return RedirectToAction("Transaction");
        }
    }
}
